﻿using ConfExpert.Forms.Base;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraGrid;
using DevTeam;
using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;

namespace ConfExpert.Forms.Ithalat
{
    public partial class DosyaOnay : XForm
    {
        public DosyaOnay()
        {
            InitializeComponent();
        }

        private void DosyaOnay_Load(object sender, EventArgs e)
        {
            //btnKaydet.Visibility = ytk(17) ? DevExpress.XtraBars.BarItemVisibility.Always : DevExpress.XtraBars.BarItemVisibility.Never;
            cmbDonem.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;

            gridView1.Init("GetMasrafDosyasiOnay", cmbDonem.EditValue.ToInt());
            gridView1.AddDetailGrid(gridView2, "REFERANS", 1);
            gridView1.AddDetailGrid(gridView3, "REFERANS", 2);

            gridView2.AddColumn("Checked", typeof(string));
            cmbDonem.EditValueChanged += cmbDonem_EditValueChanged;

            gridView1.BestFitColumns();
            gridView2.BestFitColumns();
            gridView3.BestFitColumns();



            gridView1.FormatAsNumber(2, "Masraf");
            gridView2.FormatAsNumber(2, "Tutar");

            //gridView2.Columns["GercekTutar"].DisplayFormat.FormatString = "#,##0.00;-#,##0.00;0.00";

            gridView1.AddSummary("Masraf");
            gridView2.AddSummary("Tutar");
            gridView3.AddSummary();
            gridView2.MakeColumnInvisible("tbl", "ID");

            //gridView3.Columns["Sorun"].ColumnEdit = new RepositoryItemMemoExEdit();
            //((RepositoryItemMemoExEdit)gridView3.Columns["Sorun"].ColumnEdit).ShowIcon = false;
            //gridView3.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.Bottom;
            //gridView3.ShowingEditor += gridView2_ShowingEditor;

            gridView1.SetViewCaption("Özet");
            gridView2.SetViewCaption("Detay");
            gridView3.SetViewCaption("Hata Bildirimleri");
            splitContainerControl2.AutoSize();
            gridView1.FocusedRowChanged += gridView1_FocusedRowChanged;


            //gridView1.EnableRowSelection();
            //gridView1.MakeRowSelectable();
            gridView1.MakeReadOnly();
            gridView2.MakeReadOnly("Tutar", "Kod", "Aciklama", "HesapPlaniId", "Checked");
            gridView2.EnableNewRow();
            gridView2.DataTable().TableNewRow += DosyaOnay_TableNewRow;
            gridView3.MakeReadOnly("Oncelik", "Sorun");

            riHyperLink.SingleClick = false;
            gridView1.Columns["Icerik"].ColumnEdit = riHyperLink;
            gridView1.RowCellClick += gridView1_RowCellClick;

            gridView1.RowStyle += gridView2_RowStyle;
            gridView1.OptionsView.EnableAppearanceOddRow =
            gridView1.OptionsView.EnableAppearanceEvenRow = false;

            gridView1.BestFitColumns();
            gridView2.BestFitColumns();
            gridView3.BestFitColumns();

            btnKaydet.ItemClick += btnKaydet_ItemClick;
            gridView2.DisableRowDelete();
            gridView3.DataTable().TableNewRow += CrossCheck_TableNewRow;

            GridManager gm = GridManager.GetManager(gridView1);
            gm.GridMenu.AddMenu("Onayla", onayla);
            gm.GridMenu.AddMenu("Onayı Geri Al", gerial);
            gm.GridMenu.AddMenu("Commercial Invoice'ları Göster", EditFaturaBilgi);

            gm.GridMenu.AddMenu("UNSPED'e Hata Bildir", hata);

        }

        void EditFaturaBilgi(object sender, EventArgs e)
        {
            gridView1.EndEditing();
            DataRow r = gridView1.GetFocusedDataRow();
            if (r != null)
                if (!UNSPEDMi(r["REFERANS"].ToStr()))
                {
                    using (BarsanDosyaEdit bde = new BarsanDosyaEdit(r["REFERANS"].ToStr()))
                        if (bde.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                            Rfrsh();
                }
                else
                    ShowErrorMessage("Bu opsiyon, BARSAN dosyaları içindir.");

        }

        void DosyaOnay_TableNewRow(object sender, DataTableNewRowEventArgs e)
        {
            e.Row["Checked"] = "ekle";
            e.Row["tbl"] = "DEK";
        }

        bool UNSPEDMi(string Referans)
        {
            return (Referans == string.Empty ||
                    (Referans.CompareTo("KRB.T6.0389") == 1 &&
                        Referans != "KRB.T6.0415" &&
                        Referans != "KRB.T6.0455" &&
                        Referans != "KRB.T6.0427" &&
                        Referans != "KRB.T6.0432" &&
                        Referans != "KRB.T6.0434" &&
                        Referans != "KRB.T6.0471" &&
                        Referans != "KRB.T6.0442" &&
                        Referans != "KRB.T6.0445" &&
                        Referans != "KRB.T6.0453") ||   //390 ve sonrası için -> 1
                    Referans == "KRB.T6.0370" ||
                    Referans == "KRB.T6.0373" ||
                    Referans == "KRB.T6.0374" ||
                    Referans == "KRB.T6.0378" ||
                    Referans == "KRB.T6.0382" ||
                    Referans == "KRB.T6.0386" ||
                    Referans == "KRB.T6.0388");
        }

        private void hata(object sender, EventArgs e)
        {
            gridView1.EndEditing();
            DataRow r = gridView1.GetFocusedDataRow();
            if (r != null)
                if (UNSPEDMi(r["REFERANS"].ToStr()))
                    using (UnspedeHataBildir uh = new UnspedeHataBildir(r["DosyaNo"].ToStr(), r["REFERANS"].ToStr()))
                        uh.ShowDialog();
                else
                    ShowErrorMessage("Bu opsiyon, 2016 ve sonrası UNSPED dosyaları içindir.");
        }

        private void onayla(object sender, EventArgs e)
        {
            gridView1.EndEditing();
            foreach (int handle in gridView1.GetSelectedRows())
                gridView1.SetRowCellValue(handle, "Confirmed", "E");
        }

        private void gerial(object sender, EventArgs e)
        {
            gridView1.EndEditing();
            foreach (int handle in gridView1.GetSelectedRows())
                gridView1.SetRowCellValue(handle, "Confirmed", null);
        }

        void CrossCheck_TableNewRow(object sender, DataTableNewRowEventArgs e)
        {
            e.Row["Oncelik"] = "Yüksek";    // gridView1.GetFocusedDataRow()["REFERANS"];
        }

        void gridView2_ShowingEditor(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (gridView3.FocusedRowHandle < 0 ||
                gridView3.FocusedRowHandle == GridControl.NewItemRowHandle)
                e.Cancel = false;
            else
                e.Cancel = (gridView3.GetFocusedDataRow()["UNS_TicketId"].ToInt() > 0);
        }

        void btnKaydet_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            gridView2.EndEditing();

            string str1 = gridView1.SerializeChanges();
            string str2 = gridView2.SerializeChanges();
            DataSet eksik = ExecuteDataSet("UpdIthalat_Onay2", str1, str2);

            if (eksik.Tables.Count > 0)
                using (BarsanDosyaEkle bde = new BarsanDosyaEkle(eksik))
                    if (bde.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                        ExecuteDataTable("UpdIthalat_Onay2", str1, str2);

            Rfrsh();
        }

        RepositoryItemHyperLinkEdit riHyperLink = new RepositoryItemHyperLinkEdit();

        void gridView1_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {
            if (e.RowHandle < 0)
                return;

            Cursor.Current = Cursors.WaitCursor;

            if (e.Column.FieldName == "Icerik")
            {
                DataRow dr = gridView1.GetDataRow(e.RowHandle);
                string REFERANS = dr["REFERANS"].ToStr();
                if (REFERANS != string.Empty &&
                    dr["Icerik"].ToStr() != string.Empty)
                {
                    DataTable dtDosya = ExecuteDataTable("GetIthalatDosya", REFERANS);

                    byte[] bytes = (byte[])dtDosya.Rows[0]["Dosya"];
                    string FileName = dtDosya.Rows[0]["FileName"].ToStr();

                    DevTeam.FileManager.RunTempFile(FileName, bytes);
                }
            }

            Cursor.Current = Cursors.Default;
        }

        void gridView2_RowStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e)
        {
            bool farkli = gridView1.GetRowCellValue(e.RowHandle, "Confirmed").ToStr() != "E";
            //bool farkli2 = gridView2.GetRowCellValue(e.RowHandle, "WSTutar").ToInt() !=
            //    gridView2.GetRowCellValue(e.RowHandle, "GercekTutar").ToInt();
            if (farkli && e.RowHandle > -1)
                e.Appearance.BackColor = Color.MistyRose;
        }

        void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            if (gridView1.GetFocusedDataRow() != null)
            {
                bool Barsan = !UNSPEDMi(gridView1.GetFocusedDataRow()["REFERANS"].ToStr());

                gridView2.OptionsView.NewItemRowPosition = Barsan ? DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.Bottom : DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.None;
            }

            gridView2.BestFitColumns();
            gridView3.BestFitColumns();
        }

        void cmbDonem_EditValueChanged(object sender, EventArgs e)
        {
            Rfrsh();
        }

        private void Rfrsh()
        {
            string REFERANS = gridView1.GetFocusedDataRow()["REFERANS"].ToStr();
            gridView1.RefreshDataSource("GetMasrafDosyasiOnay", cmbDonem.EditValue.ToInt());
            gridView1.FocusedRowHandle = gridView1.LocateByDisplayText(0, gridView1.Columns["REFERANS"], REFERANS);
            gridView1.MakeRowVisible(gridView1.FocusedRowHandle);

            gridView1.BestFitColumns();
            gridView2.BestFitColumns();
            gridView3.BestFitColumns();
        }
    }
}
